package com.itheima.service.Impl;

import com.itheima.dao.OrderMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.dto.OrderDTO;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {


	@Autowired
	private OrderMapper orderMapper;

	@Override
	public Order findById(Integer id) {

		Order order = orderMapper.findById(id);
		return order;

	}

	@Override
	@Transactional
	public Integer submitOrder(OrderDTO orderDTO) {
		// 预约用户不是会员，则自动注册为会员(根据身份证id，判断是否注册)
		Member member = orderMapper.findByPhone(orderDTO.getTelephone());
		Order order = new Order();

		// 有可能是帮别的用户预约
		if (member == null) {
			Member member1 = new Member();
			member1.setIdCard(orderDTO.getIdCard());
			member1.setName(orderDTO.getName());
			member1.setSex(orderDTO.getSex());
			member1.setPhoneNumber(orderDTO.getTelephone());
			member1.setRegTime(new Date());

			orderMapper.add(member1);
			order.setMemberId(member1.getId());
		} else {
			order.setMemberId(member.getId());
		}

		order.setOrderDate(orderDTO.getOrderDate());
		order.setSetmealId(orderDTO.getSetmealId());
		// 通过小程序方式进行预约，预约方式：微信预约
		order.setOrderType(Order.ORDERTYPE_WEIXIN);
		order.setOrderStatus(Order.ORDERSTATUS_NO);

		Integer recordCount = orderMapper.insert(order);
		return recordCount;
	}
}
